package Demo2;

import java.util.Objects;

public class Student implements Cloneable {
    public String name;
    public int age;

    ClassRoom classRoom;

    public Student(String name, int age, ClassRoom classRoom) {
        this.name = name;
        this.age = age;
        //会将地址直接赋值
        this.classRoom = classRoom;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", classRoom=" + classRoom.classRoom +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //先拷贝出来一个整体
        Student temp = (Student) super.clone();
        //将内部引用对象拷贝一份
        ClassRoom clone = (ClassRoom) this.classRoom.clone();
        //赋值到整体对象上
        temp.classRoom = clone;
        //返回对象
        return temp;

    }

    @Override
    public boolean equals(Object o) {

        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name) && Objects.equals(classRoom, student.classRoom);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, classRoom);
    }
}
